<?php

    namespace lactone\Core\network;

    use lactone\Core\Object;

    /**
     * 服务端网络监听工具
     * Class ServerInstance
     * @package lactone\Core\network
     */
    class ServerInstance extends Object {

        public $ip;

        public $port;

        public $ServerTool;

        protected $Process;

        /**
         * Server 监听的配置项目
         * @var bool
         */
        public $Config = false;

        /**
         *
         * @param bool $use_process
         * @param $mode
         * @param $sock_type
         */
        public function Listening($use_process = false,$mode =SWOOLE_BASE,$sock_type = SWOOLE_SOCK_TCP){

            if($use_process){
                $Object = $this;
                $this->Process = new \swoole_process(function($worker) use ($Object,$mode,$sock_type){

                    \Lactone::info("Run in Process!");

                    $Object->_listening($mode,$sock_type);
                });

                if($pid = $this->Process->start()){
                    \Lactone::saveProcessPid($pid);
                }else{
                    exit("Server Listening faild!Error:".\swoole_strerror());
                }

            }else{
                $this->_listening($mode,$sock_type);
            }

        }

        /**
         * 设置server的配置项
         * @param array $Config
         */
        protected function set($Config){
            if(is_array($Config)){
                $this->Config = $Config;
            }
            return $this;
        }

        /**
         * 监听数据
         * @param $mode
         * @param $sock_type
         */
        protected function _listening($mode = SWOOLE_BASE,$sock_type = SWOOLE_SOCK_TCP){

            $this->log("Server listening:".$this->ip.$this->port);

            $this->ServerTool = new \swoole_server($this->ip, $this->port, $mode, $sock_type);

            if($this->Config && is_array($this->Config)){
                $this->ServerTool->set($this->Config);
            }

            foreach(['Start','Receive','Shutdown','Task','Finish','Close'] as $Event){
                $EventName = "on".$Event;
                $this->ServerTool->on($Event,array($this,$EventName));
            }


            $this->ServerTool->start();
        }

        /**
         * 服务器启动事件
         * @param $server
         */
        public function onStart($server){
            \Lactone::debug($this->logMessage("Server Start"));
        }

        /**
         * 执行任务
         */
        public function onTask($serv,$task_id,$from_id, $data){
            \Lactone::debug($this->logMessage("onTask"));
        }

        /**
         * 任务执行完成
         */
        public function onFinish($serv,$task_id, $data){
            \Lactone::debug($this->logMessage("onFinish"));
        }

        /**
         * 服务器获取到数据事件
         * @param $server
         * @param $fd
         * @param $from_id
         * @param $data
         */
        public function onReceive($server,$fd,$from_id,$data){
            \Lactone::debug($this->logMessage("onReceive"));
        }

        public function onClose( $server,  $fd,  $from_id){
            \Lactone::info($this->logMessage("onClose"));
        }

        /**
         * 系统关闭事件
         * @param swoole_server $server
         */
        public function onShutdown(swoole_server $server){
            \Lactone::info($this->logMessage("onShutdown"));
        }


        /**
         * 把sockReceive获取到的数据弄成正确的数据
         * @param $data
         */
        protected function getData($data){
            $datas =  explode(\Lactone::SOCKET_TAG,$data);
            return array_filter($datas,function($var){
                return !empty($var);
            });
        }
    }